package com.editso.haier.service;

import com.editso.haier.dao.UserDao;
import com.editso.haier.entity.User;
import com.editso.haier.result.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserService {

    private UserDao userDao;

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public Result login(String name, String password, boolean remember){
        try {
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(name, password);
            token.setRememberMe(remember);
            subject.login(token);
        }catch (AuthenticationException e){
            return Result.failure("帐号或密码错误");
        }
        return Result.success();
    }

    public Result register(User user){
        return Result.promise(callback->{
            System.out.println(user);
            callback.error(r->Result.failure("帐号或密码不合法!"));
            // TODO 先不写帐号注册
//            userDao.save(user);
        });
    }

    public Result queryInfo(){

        return null;
    }
}
